package com.example.edwincompose.feature.main

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.example.edwincompose.component.MyNavigationBar
import com.example.edwincompose.feature.discovery.DISCOVERY_ROUTE
import com.example.edwincompose.feature.discovery.DiscoveryRoute
import com.example.edwincompose.feature.feed.FeedRoute
import com.example.edwincompose.feature.me.MeRoute
import com.example.edwincompose.feature.shortvideo.ShortVideoRoute
import kotlinx.coroutines.launch

@Composable
fun MainRoute() {
    MainScreen()
}

@Composable
fun MainScreen() {
    //tab

    //当前选中的界面名称
    var currentDestination by rememberSaveable {//rememberSaveable滚动列表的时候。。。啥意思。。
        mutableStateOf(TopLevelDestination.DISCOVERY.route)
    }

    //设置显示第几页
    val pageState = rememberPagerState {
        4
    }

    val scope = rememberCoroutineScope(

    )
    Column (
        Modifier.fillMaxSize()
    ){
        //类似viewpager
        HorizontalPager(
            state = pageState,
            userScrollEnabled = false,
            beyondViewportPageCount = 4,//加载屏幕外的更多页面数量
            modifier = Modifier
                .fillMaxWidth()
                .weight(1f)//高度占满剩余空间
        ) { page ->
            when (page) {
                0 -> DiscoveryRoute()
                1 -> ShortVideoRoute()
                2 -> MeRoute()
                3 -> FeedRoute()

            }
        }
        //底部导航栏
        MyNavigationBar(
            destinations = TopLevelDestination.entries,
            currentDestination = currentDestination,
            onNavigateToDestination = {index->
                currentDestination = TopLevelDestination.values()[index].route
                Log.d("wenlong", "MainScreen: "+TopLevelDestination.values()[index].route)
                scope.launch {
                    pageState.scrollToPage(index)

                }
            },
            modifier = Modifier
        )

    }

}